package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.hy.corecode.idgen.WFGIdGenerator;
import io.xxx.xbutler.data.RobotRoomMapper;
import io.xxx.xbutler.data.RoomMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RobotRoom;
import io.xxx.xbutler.domain.Room;
import io.xxx.xbutler.domain.RoomType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * 扫码入群回调
 * <p>
 * 机器人入群方式为扫码入群，则收到扫码入群回调。
 *
 * <pre>
 * {
 *     "nType": 4504,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcChatRoomId": "",
 *         "vcChatRoomSerialNo": "",
 *         "nUserCount": 0,
 *         "vcChatRoomName": "",
 *         "vcChatRoomBase64Name": "",
 *         "vcAdminWxId": "",
 *         "vcChatRoomQRCode": "",
 *         "dtCreateDate": "",
 *         "IsEnterpriseChatroom": false
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4504Handler implements CallbackHandler {

    @Resource
    private WFGIdGenerator idGenerator;

    @Resource
    private TransactionTemplate transactionTemplate;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private RobotRoomMapper robotRoomMapper;

    @Override
    public String name() {
        return "扫码入群回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4504;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        JSONObject data = (JSONObject) callback.getData();

        String roomId = data.getString("vcChatRoomId");
        String roomSerialNo = data.getString("vcChatRoomSerialNo");

        Room room = new Room()
                .setId(roomId)
                .setSerialNo(roomSerialNo)
                .setName(data.getString("vcChatRoomName"))
                .setBase64Name(data.getString("vcChatRoomBase64Name"))
                .setMemberCount(data.getInteger("nUserCount"))
                .setOwnerId(data.getString("vcAdminWxId"))
                .setQrcode(data.getString("vcChatRoomQRCode"))
                .setType(data.getBoolean("IsEnterpriseChatroom") ? RoomType.WECOM : RoomType.WECHAT)
                .setCreatedTime(now)
                .setUpdatedTime(now);

        RobotRoom robotRoom = new RobotRoom()
                .setId(idGenerator.next())
                .setRobotId(callback.getRobotId())
                .setRobotSerialNo(callback.getRobotSerialNo())
                .setRoomId(roomId)
                .setRoomSerialNo(roomSerialNo)
                .setJoinTime(LocalDateTime.ofInstant(data.getInstant("dtCreateDate"), ZoneId.systemDefault()))
                .setCreatedTime(now)
                .setUpdatedTime(now);

        transactionTemplate.executeWithoutResult(_ -> {
            roomMapper.insert(room);
            robotRoomMapper.insert(robotRoom);
        });
    }
}
